﻿using System;

namespace task2
{
    //定义信用卡类
    class CreditCard
    {
        private int _Arrears,_RepayAmount;

        public CreditCard(int Arrears,int RepayAmount)
        {
            this._Arrears = Arrears;
            this._RepayAmount = RepayAmount;
        }

        public void repay()
        {
            this._Arrears = this._Arrears + this._RepayAmount;
            Console.WriteLine("信用卡已经还款！剩余还款金额：{0}",this._Arrears);
        }
    }
    class User
    {
        
    }
    //定义银行卡类
    class BankCard
    {
        private int _Balance, _RepayAmount;

        public BankCard(int Balance,int RepayAmount)
        {
            this._Balance = Balance;
            this._RepayAmount = RepayAmount;
        }

        public void repay()
        {
            if (this._Balance < this._RepayAmount)
            {
                Console.WriteLine("余额不足！无法还款");
            }
            else
            {
                this._Balance = this._Balance - this._RepayAmount;
                Console.WriteLine("银行卡已经自动扣款！余额：{0}",this._Balance);
            }


        }

    }
    //定义银行类
    class Bank
    {
        int _RepayDate;

        public Bank(int  repayDate)
        {
            this._RepayDate = repayDate;
        }

        public delegate void MeDelegate();
        
        public event MeDelegate NotifyUsers;
        
        public void Notify()
        {


            if (DateTime.Today.Day == this._RepayDate)
            {
                if (NotifyUsers != null)
                {
                    NotifyUsers();
                }
            }
            else
            {
                Console.WriteLine("未到还款日，无须还款！");
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            /*目标
             1.使用委托实现信用卡定时还款*/
            
            /*思路
             1.创建一个 银行卡类 信用卡类 银行类
             2.设定还款事件，即银行每天查询今天是否是还款日 
             3.如果是还款日 所有委托了该事件的用户 会从他的银行卡划款至信用卡进行还款*/
            
            //定义银行对象
            Bank bank1 = new Bank(8);
            //定义信用卡对象
            CreditCard creditCard1 = new CreditCard(-10000, 400);
            //定义银行卡对象
            BankCard bankCard1 = new BankCard(28722, 400);
            //让两张银行卡都订阅事件
            bank1.NotifyUsers += new Bank.MeDelegate(bankCard1.repay);
            bank1.NotifyUsers += new Bank.MeDelegate(creditCard1.repay);
            //开启自动扣款
            Console.WriteLine("设置还款日期为每月八号：");
            bank1.Notify();
        }
    }
}